class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> result;
        queue<TreeNode*> que;
        if (root != NULL) que.push(root); // 初始化队列
        while (!que.empty()) {
            int size = que.size(); // 记录这一层的节点数
            int maxValue = INT_MIN;
            while (size--) {
                TreeNode* node = que.front();
                que.pop();
                maxValue = max(maxValue, node->val);
                // 将当前节点的子节点加入队列
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
            result.push_back(maxValue);
        }
        return result;
    }
};